/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 */
package org.javaongems.client.macctrls;

import org.javaongems.client.stdctrls.Image;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.UIObject;

/**
 * <p>Provides a fish eye item's specification which is similar to menu item.</p>
 * <p>Developer warning:<br/>
 * This was my very first GWT widget that I created just a month after GWT was 
 * first released. At that stage I consider myself lucky to able to spell GWT. 
 * You should know in advance that I am a severely impaired javascript hacker 
 * and I consider this code a minor miracle. This use to be a nested class of
 * FishEyeBar but in a rage of desparation caused by the GWT Compiler's inability
 * to report a root problem I removed all nested classes from my solutions.
 * </p>
 * <p>Just in case you are curious of the dhamma message accompanied on the license,
 * yes it is true; being an early adopter on GWT did it to me.
 * </p>
 * 
 * @author Ashin Wimalaeewa (ash)
 * 
 * This code was ported from the Dojo Toolkit::FisheyeList.js:
 * URL: http://svn.dojotoolkit.org/dojo/trunk/src/widget/FisheyeList.js
 * Credits: Cal, Alex, Bill, David & Mumme (from svn log history)
 * Apologies in advance if I have not done the Dojo FisheyeList justice
 * in my implementation. 
 */
public class FishEyeItem extends UIObject {
	final protected FishEyeBar bar;
	protected Command command;
	protected Element itemOuter;
	protected Image image;
	protected Label label;

	public FishEyeItem(FishEyeBar febar, String text, String imgSrc, Command cmd) {
		this.bar = febar;
		itemOuter = DOM.createDiv();
		image = new Image(imgSrc);
		label = new Label(text);
		setCommand(cmd);
		DOM.appendChild(itemOuter, image.getElement());
		DOM.appendChild(itemOuter, label.getElement());
		setElement(itemOuter);
		image.sinkEvents(Event.ONCLICK | Event.ONMOUSEOVER | Event.ONMOUSEOUT);
		setStyleName("gems-FishEyeItem");
		image.setStyleName("gems-FishEyeItemImage");
		image.setWidth(bar.itemWidth + "px");
		image.setHeight(bar.itemHeight + "px");
		label.setStyleName("gems-FishEyeItemLabel");
		label.setWordWrap(true);
		label.setVisible(false);
	}
	
	public void setCommand(Command cmd) {
		command = cmd;
	}

	public Command getCommand() {
		return command;
	}

	public void setCaption(String text) {
		label.setText(text);
	}
	
	public String getCaption() {
		return label.getText();
	}
	
	public void setImage(Image img) {
		image = img;
	}
	
	public Image getImage() {
		return image;
	}
	
	public Label getLabel() {
		return label;
	}

	public Element getImageElement() {
		return image.getElement();
	}
}